from helper import *

import wx, re
import wx.richtext as rt


class EditFrame(wx.MiniFrame):
	def __init__(self, parent, title, pos, size):
		wx.Frame.__init__(self, parent, -1, title, pos=pos, size=size, style=wx.NO_BORDER|wx.STAY_ON_TOP)
		
		#-------------------------------------------------------------------------
		# Panel
		#-------------------------------------------------------------------------
		# Now create the Panel to put the other controls on.
		panel = wx.Panel(self, size=size)
		
	#		self.MakeMenuBar()
		
		
		border = 0
		new_size = (size[0]-border * 2, size[1]-border * 2 - 4)
	#		note_text = rt.RichTextCtrl(panel, -1, size=new_size, style=wx.TE_MULTILINE|wx.NO_BORDER)#|wx.TE_NO_VSCROLL)
		note_text = wx.TextCtrl(panel, -1, size=new_size, style=wx.TE_MULTILINE|wx.NO_BORDER)#|wx.TE_NO_VSCROLL)
		self.note_text = note_text
		
		os = wx.GetOsDescription()		  
		if re.search('Win', os, re.I):
			note_text.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.NORMAL))

		#-------------------------------------------------------------------------
		# Binding
		#-------------------------------------------------------------------------
		note_text.Bind(wx.EVT_KEY_DOWN, self.OnChar)

		#-------------------------------------------------------------------------
		# Layout
		#-------------------------------------------------------------------------
		sizer = wx.BoxSizer(wx.VERTICAL)
		sizer.Add(note_text, 0, wx.EXPAND|wx.ALL, border)
		panel.SetSizer(sizer)
		panel.Layout()
		self.panel = panel
		self.note = None
		
	def ForwardEvent(self, evt):
		self.note_text.ProcessEvent(evt)


	def MakeMenuBar(self):
		def doBind(item, handler, updateUI=None):
			self.Bind(wx.EVT_MENU, handler, item)
			if updateUI is not None:
				self.Bind(wx.EVT_UPDATE_UI, updateUI, item)


		editMenu = wx.Menu()
		doBind( editMenu.Append(wx.ID_UNDO, "&Undo\tCtrl+Z"), self.ForwardEvent, self.ForwardEvent)
		doBind( editMenu.Append(wx.ID_REDO, "&Redo\tCtrl+Y"), self.ForwardEvent, self.ForwardEvent )
		editMenu.AppendSeparator()
		doBind( editMenu.Append(wx.ID_CUT, "Cu&t\tCtrl+X"), self.ForwardEvent, self.ForwardEvent )
		doBind( editMenu.Append(wx.ID_COPY, "&Copy\tCtrl+C"), self.ForwardEvent, self.ForwardEvent)
		doBind( editMenu.Append(wx.ID_PASTE, "&Paste\tCtrl+V"), self.ForwardEvent, self.ForwardEvent)
		doBind( editMenu.Append(wx.ID_CLEAR, "&Delete\tDel"), self.ForwardEvent, self.ForwardEvent)
		editMenu.AppendSeparator()
		doBind( editMenu.Append(wx.ID_SELECTALL, "Select A&ll\tCtrl+A"), self.ForwardEvent, self.ForwardEvent ) 

		mb = wx.MenuBar()
		mb.Append(editMenu, "&Edit")
		self.SetMenuBar(mb)
		
					
	def load_note(self, note, position_last = False):
		self.note = note
		self.note_text.SetValue(note['content'])
		if position_last:
			note_text = self.note_text
			note_text.SetInsertionPointEnd()
			note_text.Refresh()

	def save_note(self):
		if not self.note_text.GetValue().strip():
			return
		
		if self.note['content'] == self.note_text.GetValue() and self.note['revision'] == self.note['last_revision']:
			return
		
		self.note['content'] = self.note_text.GetValue()
		note_controller = wx.GetApp().note
		if self.note['revision'] == 0:
			# new note			
			self.note = note_controller.add(self.note['content'])
		else:
			# save
			self.note = note_controller.edit(self.note)
			
		app = wx.GetApp()
		app.search_frame.dirty = True
		#if self.note['revision'] != self.note['last_revision']
		app.reset_revision(self.note, True)
		
		
	def OnClose(self, evt):
		self.save_note()
		self.Close()

	def OnChar(self, evt):
		code = evt.GetKeyCode()
		app = wx.GetApp()
		if evt.AltDown():
			if code == wx.WXK_ESCAPE:
				self.note = None
				app.toggle_mode()
				return
			if code in [ord('s'), ord('S')]:
				self.save_note()
				return
		evt.Skip()
			
	def set_colors(self, color_dict):
		self.panel.SetBackgroundColour(get_fake_color('edit', 0))
		self.note_text.SetBackgroundColour(get_fake_color('edit', 0))
		self.note_text.SetForegroundColour(get_color('edit', 1))
		#self.note_text.BeginTextColour(get_color('edit', 1))
		
	def set_focus(self):
		self.Raise()
		self.note_text.SetFocus()
		
		# hack for windows
		# self.note_text.SetSelection(0, 0)
		self.note_text.SetInsertionPoint(self.note_text.GetInsertionPoint())
